﻿using System;

namespace Encloser.Toolkit
{
    public class AtomicInt : Atomic<int>
    {
        public AtomicInt() : base(0) { }

        public AtomicInt(int val) : base(val) { }

        public void Increase(int num = 1)
        {
            Set(i => i + num);
        }

        public void Decrease(int num = 1)
        {
            Set(i => i - num);
        }

        public int GetAndIncrease(int num = 1)
        {
            var n = 0;
            Set(i => (n = i) + num);
            return n;
        }
        public int DecreaseAndGet(int num = 1)
        {
            var n = 0;
            Set(i => n = i - num);
            return n;
        }

        public int IncreaseAndGet(int num = 1)
        {
            var n = 0;
            Set(i => n = i + num);
            return n;
        }

        public int GetAndDecrease(int num = 1)
        {
            var n = 0;
            Set(i => (n = i) - num);
            return n;
        }

        public bool IncreaseIf(Func<int, bool> func, int num = 1)
        {
            if (func == null) return false;
            var f = false;
            Set(i => (f = func(i)) ? i + num : i);
            return f;
        }

        public bool DecreaseIf(Func<int, bool> func, int num = 1)
        {
            if (func == null) return false;
            var f = false;
            Set(i => (f = func(i)) ? i - num : i);
            return f;
        }
    }
}
